//
//  ViewControllerVM.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "ViewControllerVM.h"
#import "HttpRequest.h"
#import "Commont.h"

@implementation ViewControllerVM

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.bannerList = [[NSMutableArray alloc]init];
        self.hotCourseList = [[NSMutableArray alloc]init];
        self.activityList = [[NSMutableArray alloc]init];
        self.newsList = [[NSMutableArray alloc]init];
        self.functionList = [[NSMutableArray alloc]init];
        FunctionModel *courseModel = [[FunctionModel alloc]init];
        courseModel.title = @"课程";
        courseModel.imageName = @"home_lesson";
        
        FunctionModel *questionnaireModel = [[FunctionModel alloc]init];
        questionnaireModel.title = @"问卷调查";
        questionnaireModel.imageName = @"home_questionnaire";
        
        FunctionModel *activityModel = [[FunctionModel alloc]init];
        activityModel.title = @"活动";
        activityModel.imageName = @"home_activity";
        
        FunctionModel *O2OModel = [[FunctionModel alloc]init];
        O2OModel.title = @"O2O";
        O2OModel.imageName = @"home_O2O";
        
        [self.functionList addObject:courseModel];
        [self.functionList addObject:questionnaireModel];
        [self.functionList addObject:activityModel];
        [self.functionList addObject:O2OModel];

        
    }
    return self;
}

- (void)requestDataWithSuccess:(void(^)(void))success failure:(void(^)(NSString *errorMessage))failure{
    
    NSDictionary *parameters = @{
                                 @"pageIndex":@(1),
                                 @"pageSize":@(10),
                                 @"languageType":@(1)
                                 };
    
    __weak typeof(self) weakSelf = self;
    
    [HttpRequest homeRequestWithParameters:parameters success:^(id  _Nonnull responseObject) {
        DebugLog(@"返回数据%@",responseObject);
        id data = responseObject[@"data"];
        //bannerList
        id bannerList = data[@"bannerList"];
        [weakSelf.bannerList removeAllObjects];
        for (NSDictionary *dic in bannerList) {
            BannerModel *model = [[BannerModel alloc]init];
            [model setValueWithData:dic];
            [weakSelf.bannerList addObject:model];
        }
        
        //推荐课
        [weakSelf.hotCourseList removeAllObjects];
        id courseList = data[@"courseList"];
        for (NSDictionary *dic in courseList) {
            HotCourseModel *model = [[HotCourseModel alloc]init];
            [model setValueWithData:dic];
            [weakSelf.hotCourseList addObject:model];
        }
        
        //近期活动
        [weakSelf.activityList removeAllObjects];
        id activityList = data[@"activityList"];
        for (NSDictionary *dic in activityList) {
            ActivityModel *model = [[ActivityModel alloc]init];
            [model setValueWithData:dic];
            [weakSelf.activityList addObject:model];
        }
        
        //资讯
        [weakSelf.newsList removeAllObjects];
        id viewsList = data[@"viewsList"];
        for (NSDictionary *dic in viewsList) {
            NewsModel *model = [[NewsModel alloc]init];
            [model setValueWithData:dic];
            [weakSelf.newsList addObject:model];
        }
                
        success();
        
    } failure:^(NSString * _Nonnull errorMessage) {
        failure(errorMessage);
    }];
}



@end
